//
//  MenuJingXuanMainViewController.m
//  Products_B
//
//  Created by lanou on 16/6/4.
//  Copyright © 2016年 朋学友. All rights reserved.
//
//  http://api.izhangchu.com/
//  is_marrow=1&methodName=ShequList&page=1&size=10&token=&user_id=&version=4.4
#import "MenuJingXuanMainViewController.h"
#import "MenuJingXuanModel.h"
#import "MenuDarenZuoPIngModel.h"
#import "MenuJingXuanCollectionViewCell.h"
@interface MenuJingXuanMainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong)NSMutableArray *modelArray;
@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSString *page;
@property (nonatomic, strong)NSMutableDictionary *parDic;
@end

@implementation MenuJingXuanMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.modelArray = [NSMutableArray array];
    self.page = @"1";
    self.parDic = @{@"is_marrow":@"1",@"methodName":@"ShequList",@"page":self.page,@"size":@"10",@"token":@"",@"user_id":@"",@"version":@"4.4"}.mutableCopy;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) collectionViewLayout:flowLayout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[MenuJingXuanCollectionViewCell class] forCellWithReuseIdentifier:@"MenuJingXuanCollectionViewCell"];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = @"1";
        self.parDic[@"page"] = self.page;
        [self requestData];
    }];
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page = [NSString stringWithFormat:@"%ld",[self.page integerValue] + 1];
        self.parDic[@"page"] = self.page;
        [self requestData];
    }];
    
    
    [self.view addSubview:_collectionView];
    
    [self requestData];
}
- (void)requestData{
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:self.parDic finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if ([self.page integerValue] == 1) {
            [self.modelArray removeAllObjects];
        }
        
        NSArray *array = [MenuJingXuanModel modelConfigureWithDic:jsonDic];
        for (MenuJingXuanModel *model in array) {
            [self.modelArray addObject:model];
        }
        NSLog(@"%@",self.modelArray);
        
        
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        [self.collectionView reloadData];
    } error:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.modelArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MenuJingXuanCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MenuJingXuanCollectionViewCell" forIndexPath:indexPath];
    MenuJingXuanModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(kWidth/2 - 1, kWidth/2-1 + 60);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 2;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 20;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
